import { Card, Button, Checkbox, Form, Input, message } from 'antd'
import { useRef } from 'react'
import { useDispatch } from 'react-redux'
import { useHistory, useLocation } from 'react-router-dom'

import { login } from '@/store/actions'

import logo from '@/assets/logo.png'
import styles from './index.module.scss'

const Login = () => {
  const checkboxRef = useRef()
  const dispatch = useDispatch()
  const history = useHistory()
  const location = useLocation()

  const onChange = ({ target }) => {
    if (target.checked) {
      checkboxRef.current._reactInternals.sibling.alternate.stateNode.style.textDecoration =
        'none'
      checkboxRef.current._reactInternals.sibling.alternate.stateNode.style.color =
        '#000'
    }
  }

  const onFinish = async (values) => {
    if (values.agree === false) {
      checkboxRef.current._reactInternals.sibling.alternate.stateNode.style.color =
        'red'
      checkboxRef.current._reactInternals.sibling.alternate.stateNode.style.textDecoration =
        'line-through'
      return
    }
    try {
      await dispatch(login({ mobile: values.mobile, code: values.code }))
      message.success('登陆成功！', 1.3, () => {
        history.replace(location.state?.from ?? '/home')
      })
    } catch (err) {
      message.warning(err.response?.data?.message ?? '网络繁忙，请稍后再试！')
    }
  }

  return (
    <div className={styles.root}>
      <Card
        className="login-wrapper"
        bordered={false}
        style={{ width: 440 }}
        bodyStyle={{ padding: '20px' }}
      >
        <img className="logo" src={logo} alt="极客园" />
        {/* 表单 */}
        <Form
          size="large"
          validateTrigger={['onBlur', 'onChange']}
          onFinish={onFinish}
          initialValues={{
            mobile: '13911111111',
            code: '246810',
            agree: true,
          }}
        >
          <Form.Item
            name="mobile"
            rules={[
              { required: true, message: '手机号不能为空！' },
              {
                pattern: /^1[3|4|5|7|8|9]\d{9}$/,
                message: '手机号格式不正确！',
                validateTrigger: 'onBlur',
              },
            ]}
          >
            <Input maxLength={11} placeholder="请输入手机号" />
          </Form.Item>

          <Form.Item
            name="code"
            rules={[
              { required: true, message: '验证码不能为空！' },
              {
                len: 6,
                message: '手机号格式不正确！',
                validateTrigger: 'onBlur',
              },
            ]}
          >
            <Input maxLength={6} placeholder="请输入验证码" />
          </Form.Item>

          <Form.Item
            name="agree"
            valuePropName="checked"
            rules={[
              {
                required: true,
                message: '请仔细阅读『用户协议』和『隐私条款』后 √',
              },
            ]}
          >
            <Checkbox ref={checkboxRef} onChange={onChange}>
              我已阅读并同意『用户协议』和『隐私条款』
            </Checkbox>
          </Form.Item>

          <Form.Item style={{ marginBottom: '7px' }}>
            <Button type="primary" htmlType="submit" block>
              登录
            </Button>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}

export default Login
